package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    @RequestMapping("/findPage")
    //分页查询角色信息
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        return roleService.findPage(queryPageBean);
    }

    @RequestMapping("/add")
    //添加角色
    public Result add(Integer[] permissionIds,Integer[] menuIds,@RequestBody Role role){
        System.out.println(permissionIds+","+menuIds+","+role);
        try {
            roleService.add(permissionIds, menuIds, role);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    @RequestMapping("/findById")
    //用于回写角色数据
    public Result findById(Integer id){
        try{
          Role role =  roleService.findById(id);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,role);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }




    @RequestMapping("/edit")
    public Result edit(Integer[] permissionIds,Integer[] menuIds,@RequestBody Role role){
        System.out.println(permissionIds+","+menuIds+","+role);
        try {
            roleService.edit(permissionIds,menuIds,role);
            return new Result(false,MessageConstant.EDIT_ROLE_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ROLE_FAIL);
        }

    }

    @RequestMapping("/deleteById")
    //根据id删除角色
    public Result deleteById(Integer role_id){
        try {
            Boolean flag = roleService.deleteById(role_id);
            if(!flag){
                return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
            }else {
                return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLE_ERROR);
        }
    }

    //查询所有角色
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Role> list = roleService.findAll();//发送请求
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //根据用户id查询需要勾选的角色id
    @RequestMapping("/findRoleIdsByUserId")
    public Result findRoleIdsByUserId(Integer userId){
        try{
            List<Integer> list = roleService.findRoleIdsByUserId(userId);
            System.out.println(userId);
            System.out.println(list);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }
}
